package com.hnnou.freshmenappserver.controller;

import com.hnnou.freshmenappserver.bean.User;
import com.hnnou.freshmenappserver.bean.vo.UpdatePasswordVO;
import com.hnnou.freshmenappserver.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*",
        methods = {RequestMethod.GET, RequestMethod.OPTIONS, RequestMethod.POST})
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/freshmenapp/user/login")
    public Map<String, Object> login(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        User user1 = userService.login(user);
        if (user1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", user1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }

    @RequestMapping("/freshmenapp/user/register")
    @ResponseBody
    public Map<String, Object> addMember(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        int count = userService.register(user);
        if (count > 0) {
            map.put("success", true);
            map.put("msg", "成功");
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }
    @RequestMapping("/freshmenapp/user/updatepassword")
    @ResponseBody
    public Map<String, Object> updatePassword(@RequestBody UpdatePasswordVO updatePasswordVO) {
        Map<String, Object> map = new HashMap<>();
        int count = userService.updatePassword(updatePasswordVO);
        if (count > 0) {
            map.put("success", true);
            map.put("msg", "成功");
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }
    @RequestMapping("/freshmenapp/user/list")
    @ResponseBody
    public Map<String, Object> list() {
        Map<String, Object> map = new HashMap<>();
        List<User> list = userService.findAll();
        if (list != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("list",list);

        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }
    @RequestMapping("/freshmenapp/classmate/findone")
    @ResponseBody
    public Map<String, Object> findOneById(@RequestBody User user) {
        Map<String, Object> map = new HashMap<>();
        User user1 = userService.selectById(user.getId());
        if (user1 != null) {
            map.put("success", true);
            map.put("msg", "成功");
            map.put("data", user1);
        } else {
            map.put("success", false);
            map.put("msg", "失败");
        }
        return map;
    }
}
